/** ST表模板 */
#include <bits/stdc++.h>
using namespace std;
const int logn = 21;
const int maxn = 2000001;
int f[logn + 1][maxn], Logn[maxn + 1];
inline int read()
{
  char c = getchar();
  int x = 0, f = 1;
  while (c < '0' || c > '9')
  {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
  {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x * f;
}
void pre()
{
  Logn[1] = 0;
  Logn[2] = 1;
  for (int i = 3; i < maxn; i++)
  {
    Logn[i] = Logn[i / 2] + 1;
  }
}
int main()
{
  int n = read(), m = read();
  for (int i = 1; i <= n; i++)
    f[0][i] = read();
  pre();
  for (int j = 1; j <= logn; j++)
    for (int i = 1; i + (1 << j) - 1 <= n; i++)
      f[j][i] = max(f[j - 1][i], f[j - 1][i + (1 << (j - 1))]);
  for (int i = 1; i <= m; i++)
  {
    int x = read(), y = read();
    int s = Logn[y - x + 1];
    printf("%d\n", max(f[s][x], f[s][y - (1 << s) + 1]));
  }
  return 0;
}
